package cn.aifeix.org.controller;

import cn.aifeix.basic.anno.LogAnno;
import cn.aifeix.basic.anno.PreAuthorize;
import cn.aifeix.basic.util.AjaxResult;
import cn.aifeix.basic.util.PageList;
import cn.aifeix.org.domain.Department;
import cn.aifeix.org.query.DepartmentQuery;
import cn.aifeix.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
//@LogAnno
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 添加/修改
     */
    @PutMapping
//    @LogAnno(method = "addOrUpdate",methodDes = "添加或修改")
    @PreAuthorize(name = "添加或修改",sn = "department:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (null == department.getId()){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
//    @LogAnno(method = "delete",methodDes = "删除")
    @PreAuthorize(name = "删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
//    @LogAnno(method = "findAll",methodDes = "查询所有")
    @PreAuthorize(name = "查询所有",sn = "department:findAll")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
//    @LogAnno(method = "findById",methodDes = "通过id查询单条")
    @PreAuthorize(name = "通过id查询单条",sn = "department:findById")
    public Department findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
//    @LogAnno(method = "queryByPage",methodDes = "分页查询部门")
    @PreAuthorize(name = "分页查询部门",sn = "department:queryByPage")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        return departmentService.queryByPage(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
//    @LogAnno(method = "batchDel",methodDes = "批量删除")
    @PreAuthorize(name = "批量删除部门",sn = "department:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
//        System.out.println(ids);
        departmentService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /**
     * 部门无级树
     */
    @PostMapping("/tree")
    @PreAuthorize(name = "查询部门树",sn = "department:deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
